package com.xnyzc.lhy.order.controller.capi.user.message;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.common.constants.SystemConstant;
import com.xnyzc.lhy.order.entity.param.cms.PageDto;
import com.xnyzc.lhy.order.entity.param.user.message.DriverMessageParam;
import com.xnyzc.lhy.order.entity.param.user.message.SystemMessageDTO;
import com.xnyzc.lhy.order.entity.user.message.OaSysMessage;
import com.xnyzc.lhy.order.service.user.message.IOaSysMessageService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 用户消息列表
 * @Author Bin
 * @Date 2019/7/23 10:48
 **/
@RestController
@RequestMapping("/oa/userMessage")
public class OaUserMessageController {

    @Autowired
    private IOaSysMessageService iOaSysMessageService;

    /**
     * 根据用户id与消息类型查询相应的消息列表
     * @param messageType 消息类型 （0：系统消息、1：活动消息）
     * @param current 当前页
     * @return
     */
    @ApiOperation(value = "根据用户id与消息类型查询相应的消息列表", notes = "根据用户id与消息类型查询相应的消息列表")
    @GetMapping(value = "selectUserMessagePage")
    public Rv selectUserMessagePage(
            @ApiParam(name = "messageType", value = "消息类型(0：系统消息、1：活动消息)") @RequestParam(value = "messageType") Integer messageType,
            @ApiParam(name = "current", value = "当前页") @RequestParam(value = "current") long current
    ){
        long userId = TokenUtil.getTokenUserId();
        if (userId == 0){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        if (messageType == null){
            throw PangException.create(EErrorCode.argCheckErr);
        }
        return Rv.wrap(iOaSysMessageService.selectUserMessagePage(userId, messageType, current));
    }

    /**
     * 系统消息列表
     * @return
     */
    @ApiOperation(value = "系统消息列表", notes = "系统消息列表")
    @PostMapping(value = "selectSystemMessageList")
    public Rv selectSystemMessageList(
            @RequestBody SystemMessageDTO systemMessageDTO
    ){
        if (CheckUtil.objIsEmpty(systemMessageDTO)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        systemMessageDTO.setDriverId(TokenUtil.getTokenUserId());
        if (systemMessageDTO.getType().equals(SystemConstant.MESSAGE)) {
            return iOaSysMessageService.selectSystemMessageList(systemMessageDTO);
        } else if (systemMessageDTO.getType().equals(SystemConstant.ACTIVITY)) {
            return iOaSysMessageService.selectActivityMessageList(systemMessageDTO);
        } else {
            throw PangException.create(EErrorCode.argCheckErr);
        }
    }

    /**
     * 系统二级列表
     * @param driverMessageParam
     * @return
     */
    @PostMapping(value = "selectSystemMessage")
    public Rv selectSystemMessage(
            @RequestBody DriverMessageParam driverMessageParam
            ) {
        return iOaSysMessageService.selectSystemMessage(driverMessageParam);
    }

    /**
     * 已读消息
     * @param oaSysMessage 消息ID
     */
    @PostMapping(value = "readMessage")
    public Rv readMessage(
            @RequestBody OaSysMessage oaSysMessage
            ) {

        if (CheckUtil.objIsEmpty(oaSysMessage)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        return iOaSysMessageService.readMessage(oaSysMessage.getMessageId());
    }

    /**
     * 保存消息
     */
    @PostMapping(value = "saveMessage")
    public Rv saveMessage(@RequestBody OaSysMessage oaSysMessage) {
        return iOaSysMessageService.saveMessage(oaSysMessage);
    }

}
